﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Common.Converters
{
    /// <summary>
    /// 枚举转为bool
    /// </summary>
    public class EnumToBooleanConverter : IValueConverter
    {
        /// <summary>
        /// convert
        /// </summary>
        /// <param name="value">      </param>
        /// <param name="targetType"> </param>
        /// <param name="parameter">  </param>
        /// <param name="culture">    </param>
        /// <returns> </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return false;

            string enumValue = value.ToString();
            string targetValue = parameter.ToString();

            return enumValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// ConvertBack
        /// </summary>
        /// <param name="value">      </param>
        /// <param name="targetType"> </param>
        /// <param name="parameter">  </param>
        /// <param name="culture">    </param>
        /// <returns> </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return null;

            bool isChecked = (bool)value;
            string targetValue = parameter.ToString();

            if (isChecked)
                return Enum.Parse(targetType, targetValue);

            return null;
        }
    }
}